var ReportFunctions = Class.create();

ReportFunctions.prototype = {
	initialize : function() {
	},

	createReport : function(a) {
		this._setPublic(a);
	},

	copyReport : function(a) {
		var record = new GlideRecord('sys_report');
		record.initialize();

		// get fields form current record and prepare filling new record
		var fields = a.getFields();
		for ( var num = 0; num < fields.size(); num++) {
			var field = fields.get(num);
			if (!field.nil()) {
				record[field.getName()] = field.toString();
			}
		}

		// overwrite field in the new record and insert the record
		record.user = gs.getUserID();
		if (field.title == record.title) {
			record.title = field.title + ' '
					+ gs.getUser().getRecord().getValue('u_abbreviation');
		}
		var answer = record.insert();

		return answer;

	},

	getName : function(a) {
		// return abbreviation based on user sys_id
		var answer = 'Unknown User';
		var gr = new GlideRecord('sys_user');
		if (gr.get(a)) {
			answer = gr.u_abbreviation;

			return answer;
		}
	},

	_setPublic : function(id) {

		var list = id.roles.toString();
		var array = list.split(",");
		var len = array.length;

		var onList = false;
		for ( var i = 0; i < len; i++) {
			if (array[i] == 'public') {
				// if in the array value 'public' exists, no update on the roles
				// field
				onList = true;

			}
		}

		if (!onList) {
			// if false, add value 'public' to roles field.
			array[len] = 'public';

		}

		id.roles.setValue(array.join());

	}

};